﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace ZLDC.Estate.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ls_RoomList : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string sqlstr = "";
            //得到当前页

            string CurrentPage = context.Request["page"];
            //得到每页显示多少
            string PageShowLimit = context.Request["rp"];
            //得到主键
            string TableID = context.Request["sortname"];
            //得到排序方法
            string OrderMethod = context.Request["sortorder"];
            string xx = context.Request["txtpage"];
            //得到要过滤的字段
            string FilterField = context.Request["qtype"];
            //获取条件
            //string letter_pressed = context.Request["letter_pressed"];
            //得到要过滤的内容
            //string FilterFieldContext;
            //if (context.Request.Form["letter_pressed"] == null)
            //{
            //    FilterFieldContext = "";
            //}
            //else
            //{
            //    FilterFieldContext = context.Request["letter_pressed"];
            //}

            String searchitems = context.Request["searchitems"];
            //          获得模糊查询条件  
            //String qtype = context.Request["qtype"];  
            //得到表的总行数
            ZLDC.Common.Estate._Database db = new ZLDC.Common.Estate._Database("estate");

             DataSet ds = db.Query("select count(id) from " + FilterField.Split(',')[0] + " " );
             string TableRowCount = ds.Tables[0].Rows[0][0].ToString();
            //得到主SQL
            string[] tmp = FilterField.Split(',');
            sqlstr = "SELECT * FROM " + FilterField.Split(',')[0];
            if (!string.IsNullOrEmpty(OrderMethod) && OrderMethod != "undefined")
            {
                sqlstr +=" order by id"+ OrderMethod;
            }
            ZLDC.Common.Estate._Database db1 = new ZLDC.Common.Estate._Database("estate");
            //得到表
            DataSet returnTable = db1.Query("select count(id) from " + FilterField.Split(',')[0] + ";exec spAll_ReturnRows '" + sqlstr + "', " + Convert.ToInt32(CurrentPage) + " ," + Convert.ToInt32(PageShowLimit) + "," + TableID + "," + TableID + "");
            //DataTable returnTable = DbHelperSQL.Query("exec spAll_ReturnRows 'SELECT  * FROM PageTest', 1 ,10,id,id", ss).Tables[0];
            context.Response.Write(DtToSON2(returnTable.Tables[1], CurrentPage, TableRowCount));
        }
        /// <summary>
        /// JSON格式转换
        /// </summary>
        /// <param name="dt">DataTable表</param>
        /// <param name="page">当前页</param>
        /// <param name="total">总计多少行</param>
        /// <returns></returns>
        public static string DtToSON2(DataTable dt, string page, string total)
        {

            StringBuilder jsonString = new StringBuilder();
            jsonString.AppendLine("{");
            jsonString.AppendFormat("page: {0},\n", page);
            jsonString.AppendFormat("total: {0},\n", total);
            jsonString.AppendLine("rows: [");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonString.Append("{");
                jsonString.AppendFormat("id:'{0}',cell:[", dt.Rows[i][0].ToString());
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j == dt.Columns.Count - 1)
                    {
                        jsonString.AppendFormat("'{0}'", dt.Rows[i][j].ToString());
                    }
                    else
                    {
                        jsonString.AppendFormat("'{0}',", dt.Rows[i][j].ToString());
                    }

                    if (j == dt.Columns.Count - 1)
                    {
                        // jsonString.AppendFormat(",'{0}'", "<input type=\"button\" value=\"查看\" id=\"sss\" onclick=\"sss(" + dt.Rows[i][0].ToString() + ")\" />");
                    }

                }

                jsonString.Append("]");
                if (i == dt.Rows.Count - 1)
                {
                    jsonString.AppendLine("}");

                }
                else
                {

                    jsonString.AppendLine("},");
                }

            }

            jsonString.Append("]");
            jsonString.AppendLine("}");

            return jsonString.ToString();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
